/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FrameAdicionarProduto.java
 *
 * Created on 12/11/2010, 23:41:07
 */
package all_pet_iv.gui;

import java.awt.Image;
import java.awt.Toolkit;
import java.net.URL;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Daniel
 */
public class FrameEditarProduto extends javax.swing.JFrame {

    /** Creates new form FrameAdicionarProduto */
    public FrameEditarProduto() {
        initComponents();
        FramePrincipal.getInstance().setEnabled(false);
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        this.setLocationRelativeTo(null);
        // Caso queira maximizar this.setExtendedState(MAXIMIZED_BOTH);
        URL url = this.getClass().getResource("/all_pet_iv/gui/icons/patinha_grande.png");
        Image icon = Toolkit.getDefaultToolkit().getImage(url);
        this.setIconImage(icon);
        this.carregarComponentes();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        labelCodigo = new javax.swing.JLabel();
        jButtonSalvar = new javax.swing.JButton();
        jButtonCancelar = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        campoQuantidadeCritica = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        campoPrecoVenda = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        campoSolicitacaoPadrao = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("ALLPET - Edição de Produto");
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setLocationByPlatform(true);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jLabel1.setBackground(new java.awt.Color(153, 204, 255));
        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("EDIÇÃO DE PRODUTO");
        jLabel1.setOpaque(true);

        jLabel2.setText("Código:");

        labelCodigo.setForeground(new java.awt.Color(255, 51, 51));

        jButtonSalvar.setText("Salvar");
        jButtonSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSalvarActionPerformed(evt);
            }
        });

        jButtonCancelar.setText("Cancelar");
        jButtonCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelarActionPerformed(evt);
            }
        });

        jLabel6.setText("Quantidade Crítica:");

        campoQuantidadeCritica.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                campoQuantidadeCriticaActionPerformed(evt);
            }
        });

        jLabel7.setText("Preço de Venda(R$):");

        jLabel8.setText("Solicitação padrão:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 335, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel7)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(campoPrecoVenda, javax.swing.GroupLayout.DEFAULT_SIZE, 111, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 91, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jButtonSalvar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jButtonCancelar)))
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel8)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(campoSolicitacaoPadrao, javax.swing.GroupLayout.DEFAULT_SIZE, 111, Short.MAX_VALUE)
                        .addGap(91, 91, 91))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(campoQuantidadeCritica, javax.swing.GroupLayout.DEFAULT_SIZE, 109, Short.MAX_VALUE)
                        .addGap(91, 91, 91))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(86, 86, 86)
                        .addComponent(labelCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(91, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(38, 38, 38)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(labelCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(campoQuantidadeCritica, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(campoSolicitacaoPadrao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(campoPrecoVenda, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonSalvar)
                    .addComponent(jButtonCancelar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelarActionPerformed
        this.formWindowClosing(null);
    }//GEN-LAST:event_jButtonCancelarActionPerformed

    private void campoQuantidadeCriticaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_campoQuantidadeCriticaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_campoQuantidadeCriticaActionPerformed

    private void jButtonSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSalvarActionPerformed
        PanelEstoque pane = (PanelEstoque) FramePrincipal.getInstance().getContentPane();
        int row = pane.getTable().getSelectedRow();
        double quantidadeAtual = Double.parseDouble(pane.getTable().getModel().getValueAt(row,3)+"");
        if (campoQuantidadeCritica.getText().equals("") ||campoPrecoVenda.getText().equals("") || campoSolicitacaoPadrao.getText().equals("")) {
            JOptionPane.showMessageDialog(null, "Campo obrigatório em branco!", "Erro", JOptionPane.ERROR_MESSAGE);
        }else if (quantidadeAtual<=Double.parseDouble(campoQuantidadeCritica.getText())){
            JOptionPane.showMessageDialog(null, "Quantidade atual menor/igual que a quantidade crítica","Erro", JOptionPane.ERROR_MESSAGE);
        }else if (Double.parseDouble(campoSolicitacaoPadrao.getText())<=Double.parseDouble(campoQuantidadeCritica.getText())){//Caso a quant atual chegue a zero
            JOptionPane.showMessageDialog(null, "Solicitação padrão menor/igual que a quantidade crítica","Erro", JOptionPane.ERROR_MESSAGE);            
        }else{   
            try {
                String codigo = labelCodigo.getText();
                double quantidadeCritica = Double.parseDouble(campoQuantidadeCritica.getText());
                double precoVenda = Double.parseDouble(campoPrecoVenda.getText());
                double solicitacaoPadrao = Double.parseDouble(campoSolicitacaoPadrao.getText());
            
                try {
                    FramePrincipal.getInstance().fachada.editarProduto(codigo,quantidadeCritica,solicitacaoPadrao,precoVenda);
                    JOptionPane.showMessageDialog(null, "Produto editado com sucesso!", "Parabéns", JOptionPane.INFORMATION_MESSAGE);
                    this.formWindowClosing(null);
                    PanelEstoque panel = new PanelEstoque();
                    FramePrincipal.getInstance().setContentPane(panel);
                    FramePrincipal.getInstance().setVisible(true);

                 } catch (SQLException ex) {
                    Logger.getLogger(FrameEditarProduto.class.getName()).log(Level.SEVERE, null, ex);
                }
            
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(null, "Campo inválido!", "Erro", JOptionPane.ERROR_MESSAGE);
        }
    }
    }//GEN-LAST:event_jButtonSalvarActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        this.dispose();
        FramePrincipal.getInstance().setEnabled(true);
    }//GEN-LAST:event_formWindowClosing

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {

                new FrameEditarProduto().setVisible(true);


            }
        });
    }

    private void carregarComponentes() {
        PanelEstoque pane = (PanelEstoque) FramePrincipal.getInstance().getContentPane();
        int row = pane.getTable().getSelectedRow();
        
        this.labelCodigo.setText((String) pane.getTable().getModel().getValueAt(row, 0));
        this.campoQuantidadeCritica.setText(""+pane.getTable().getModel().getValueAt(row,4));
        this.campoSolicitacaoPadrao.setText(""+pane.getTable().getModel().getValueAt(row,5));
        this.campoPrecoVenda.setText(""+pane.getTable().getModel().getValueAt(row,7));
        
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField campoPrecoVenda;
    private javax.swing.JTextField campoQuantidadeCritica;
    private javax.swing.JTextField campoSolicitacaoPadrao;
    private javax.swing.JButton jButtonCancelar;
    private javax.swing.JButton jButtonSalvar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel labelCodigo;
    // End of variables declaration//GEN-END:variables
}
